/**
 * 
 */
package org.pandazxx.picasamanager.io;

import java.util.Formatter;

/**
 * @author zhengxx
 * 
 */
public class TransferSize {
	private long transferBytes;
	
	private static TransferSize transferSize = new TransferSize(); 
	
	public enum TransferUnit {
		Byte, KiloByte, MegaByte, GigaBtye
	}
	
	public TransferSize() {
		transferBytes = 0;
	}
	
	public TransferSize(long size) {
		transferBytes = size;
	}
	

	public static boolean isByte(long number) {
		return number < 1024;
	}

	public static boolean isKiloByte(long number) {
		return number < 1024 * 1024 && number >= 1024;
	}

	public static boolean isMegaByte(long number) {
		return number < 1024 * 1024 * 1024 && number >= 1024 * 1024;
	}

	public static boolean isGigaByte(long number) {
		return number >= 1024 * 1024 * 1024;
	}
	
	public TransferUnit getUnit() {
		if (isByte(transferBytes)) {
			return TransferUnit.Byte;
		} else if (isKiloByte(transferBytes)) {
			return TransferUnit.KiloByte;
		} else if (isMegaByte(transferBytes)) {
			return TransferUnit.MegaByte;
		} else return TransferUnit.GigaBtye;
	}
	
	public double getReadableSize() {
		if (isByte(transferBytes)) {
			return transferBytes;
		} else if (isKiloByte(transferBytes)) {
			return (double) transferBytes / 1024;
		} else if (isMegaByte(transferBytes)) {
			return (double) transferBytes / 1024 / 1024;
		} else return (double) transferBytes / 1024 / 1024 / 1024;
	}
	
	public String toString() {
		String unit;
		if (isByte(transferBytes)) {
			unit = "b";
		} else if (isKiloByte(transferBytes)) {
			unit = "Kb";
		} else if (isMegaByte(transferBytes)) {
			unit = "Mb";
		} else unit = "Gb";
		Formatter fmt = new Formatter();
		fmt.format("%.2f %s", getReadableSize(), unit);
		return fmt.toString();
		
	}
	
	public static String formatNormal(long size) {
		transferSize.setTransferBytes(size);
		return transferSize.toString();
	}

	/**
	 * @return the transferBytes
	 */
	public long getTransferBytes() {
		return transferBytes;
	}

	/**
	 * @param transferBytes the transferBytes to set
	 */
	public void setTransferBytes(long transferBytes) {
		this.transferBytes = transferBytes;
	}
	
	public static void main(String[] args) {
		for (String arg : args) {
			
			long size = Long.parseLong(arg);
			TransferSize ts = new TransferSize(size);
			System.out.println(size + ":" + ts.toString());
		}
	}
}
